Skip to content

Added Error Handling & Defensive Programming Guide#3539

Open
veblush wants to merge 1 commit into
tensorflow:mainfrom
veblush:error-doc
Open

Added Error Handling & Defensive Programming Guide#3539
veblush wants to merge 1 commit into
tensorflow:mainfrom
veblush:error-doc

Conversation

@veblush
Copy link
Copy Markdown
Collaborator

@veblush veblush commented Apr 28, 2026

BUG=n/a

@veblush veblush requested a review from a team as a code owner April 28, 2026 21:22
@veblush veblush added the type:support Documentation, general questions, or project help. label Apr 28, 2026
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌 The opening paragraph 👍

Comment on lines +50 to +51
the Setup phase. Because developers can use `TF_LITE_STRIP_ERROR_STRINGS` to
remove the string bloat in production, checking topologies in `Prepare`
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(ex. a RELEASE build)


During the `Prepare` phase, kernels should validate their inputs and parameters
to ensure `Eval` can run blindly and safely. We prioritize clear error messages
here (relying on `TF_LITE_STRIP_ERROR_STRINGS` to mitigate the ROM cost in
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(ex. a RELEASE build)

> you have multiple preconditions, combine them into a single
> `TF_LITE_ENSURE(context, a != nullptr && b != nullptr)`. **Note:** For
> production builds where ROM is severely constrained, firmware developers
> should define the `TF_LITE_STRIP_ERROR_STRINGS` macro to compile out these
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or use a RELEASE build for production systems

```

```
If it's just an invariant guaranteed by `Prepare`, ask to hoist the check or
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hoist -> discard ?

Comment on lines +214 to +240
### Fixing Fuzzer Crashes

Fuzzer fixes should align with the core philosophy. We evaluate them based on
the type of crash:

1. **Corrupted FlatBuffer Files (Working As Intended):** If the fuzzer mutates
the FlatBuffer byte array so a `Tensor` offset points beyond the end of the
file (causing a segfault), **request changes if the PR uses `TF_LITE_ENSURE`
or raw `if` statements**. To keep the binary size as small as possible, we
prefer to omit the FlatBuffer verifier. However, **it is recommended to
accept the PR if it exclusively uses `TFLITE_DCHECK`** to catch the
out-of-bounds index. This treats the structural check purely as a zero-cost
developer aid during debugging.
2. **Invalid Model Topologies (Fix in Prepare):** If the fuzzer generates a
valid FlatBuffer but modifies a `CONV_2D` operator to have 0 inputs instead
of the expected 3, **this is a good fix in `Prepare`** using
`TF_LITE_ENSURE_EQ(context, NumInputs(node), 3);`.
3. **Static Math Crashes (Fix in Prepare):** If the fuzzer sets a quantization
`scale` parameter to `0.0` (causing a hardware trap during inference),
**this is a good fix in `Prepare`** using `TF_LITE_ENSURE(context, scale !=
0.0);`.
4. **Dynamic Math/Data Crashes (Fix in Eval):** If the fuzzer provides input
data to a `GATHER` op with an index of `999` (causing out-of-bounds
corruption), or a divisor tensor evaluates to `0` (causing a divide-by-zero
trap), **this is a good fix in `Eval`**. Prefer a raw `if (index >= 10)
return kTfLiteError;` to save ROM (avoid using `TF_LITE_ENSURE` here just to
log the error; save the ROM).
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no fuzzer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type:support Documentation, general questions, or project help.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants